package com.otojob.utils;

import com.baomidou.mybatisplus.extension.api.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Description
 * @Author: TingFeng
 * @CreateTime: 2022/3/19  18:14
 */
@RestControllerAdvice
@Slf4j
public class OtoJobException {

    //指定出现什么异常执行这个方法
    @ExceptionHandler(Exception.class)
    @ResponseBody //为了返回数据
    public Results error(Exception e) {
        e.printStackTrace();

        return Results.error().message("执行了全局异常处理");
    }

    //特定异常
    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody //为了返回数据
    public Results error(ArithmeticException e) {
        e.printStackTrace();
        return Results.error().message("执行了ArithmeticException异常处理");
    }

    //自定义异常
    @ExceptionHandler(OtoException.class)
    @ResponseBody //为了返回数据
    public Results error(OtoException e) {
        e.printStackTrace();
        return Results.error().code(e.getCode()).message(e.getMsg());
    }

}
